<template>
    <view>
		<!-- #ifdef APP-PLUS -->
		<!-- 右侧icon -->
		<view class="top-share-right-icon">
		    <uni-icons type="redo" size="20" color="#333" class="icon round cp" @tap="popup_share_event"></uni-icons>
		</view>
		<!-- #endif -->
		<view class="page_big">
			<view v-if="banner_list.length > 0 && (params.category_id || 0) == 0" class="groupsbanner">
				<!-- 轮播图片 -->
				<swiper :indicator-dots="true" :autoplay="true" interval="5000" duration="1000" :style="'height:' + bannerHeight + 'px;'">
					<block v-for="(item, index) in banner_list" :key="index">
						<swiper-item>
							<image :src="item.images_url" mode="widthFix" class="swiper-image" :data-value="item.event_value || item.url" :data-type="item.event_type == undefined ? 1 : item.event_type" @tap="banner_event" />
						</swiper-item>
					</block>
				</swiper>
			</view>

			<!--分类导航-->
			<view v-if="category_list.length > 0" class="category_list">
				<scroll-view scroll-x>
					<view class="item" v-for="(item, index) in category_list" :key="index">
						<navigator :url="'/pages/fcc/groups/index/index?category_id=' + item.id+'&shop_id='+shop_id" hover-class="none">
							<view v-if="item.id == params.category_id" class="cat-img-box on"><image class="dis-block" :src="item.big_images" mode="aspectFit"></image></view>
							<view v-else class="cat-img-box"><image class="dis-block" :src="item.big_images" mode="aspectFit"></image></view>
							<view class="goods-base">
								<view v-if="item.id == params.category_id" class="goods-title single-text on">{{ item.name }}</view>
								<view v-else class="goods-title single-text">{{ item.name }}</view>
							</view>
						</navigator>
					</view>
				</scroll-view>
			</view>

			<!-- 列表 -->
			<view class="data-list oh">
				<view v-for="(item, index) in data_list" :key="index">
					<component-groups-item :propConfig="lang_info" :propData="item"></component-groups-item>
				</view>
			</view>

			<view v-if="data_list.length == 0">
				<!-- 提示信息 -->
				<component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
			</view>

			<!-- 结尾 -->
			<component-bottom-line :propStatus="data_bottom_line_status" :propMsg="data_bottom_line_msg"></component-bottom-line>

			<!-- 在线客服 -->
			<view v-if="common_app_is_online_service == 1">
				<!-- 在线客服 -->
				<component-online-service :propIsNav="true"></component-online-service>
			</view>
			
			<!-- 分享弹窗 -->
			<component-share-popup :diyTheme="diytheme" ref="share"></component-share-popup>
		</view>

        <!-- 结尾 -->
		<component-footer :propStatus="1"></component-footer>
    </view>
</template>

<script>
const app = getApp();
import componentPopup from '@/components/popup/popup';
import componentFooter from "@/pages/fcc/groups/public/footer";
import componentNoData from "@/components/no-data/no-data";
import componentBottomLine from "@/components/bottom-line/bottom-line";
import componentOnlineService from "@/components/online-service/online-service";
import componentSharePopup from "@/components/share-popup/share-popup";
import componentGroupsItem from '@/pages/fcc/groups/public/groups-item'; 

var groups_static_url = app.globalData.get_static_url('groups', true);
export default {
    components: {
        componentPopup,
		componentFooter,
		componentNoData,
		componentBottomLine,
		componentOnlineService,
		componentSharePopup,
		componentGroupsItem
    },
    data() {
        return {
			groups_static_url: groups_static_url,
            currency_symbol: app.globalData.currency_symbol(),
            load_status: 0,
            data_list_loding_status: 1,
            data_bottom_line_status: false,
            banner_list: [],
            category_list: [],
            data_list: [],
            data_page_total: 0,
            data_page: 1,
            params: null,
            post_data: {},
            bannerHeight: Math.ceil((314.96 / 640) * app.globalData.get_system_info().windowWidth),
            common_app_is_online_service: 0,
            data_total: '',
            data_list_loding_msg: '',
			shop_id:0,
			// 自定义分享信息
			share_info: {
				'title':'',
			},

            lang_info: {
                'list_need_how_many_before': '',
                'list_need_how_many_after': '',
				'list_already_buy_before': '',
				'list_already_buy_after': '',
				'list_go_to_order_text': '',
				'list_price_before': '',
				'list_price_old_before': '',
				'list_price_old_after': ''
            },
			data_bottom_line_msg:'',
			diytheme:null,
        };
    },
    onLoad(params) {
		var self = this;
		var upd_data = {
		    params: params,
		    post_data: params
		}
		if((params['shop_id'] || null) != null)
		{
			upd_data['shop_id'] = parseInt(params['shop_id']);
			// 将shop_id缓存起来，直至有新的shop_id来覆盖该值
			uni.setStorageSync('plugins_groups_shop_id', params['shop_id']);
		}else{
			// 若无传值，则看存的是否有缓存
			const value = uni.getStorageSync('plugins_groups_shop_id');
			if (value) {
				upd_data['shop_id'] = parseInt(value);
			}
		}
        this.setData(upd_data);
		self.init();
		/*setTimeout(function() {
		    
		}, 1000);*/
    },
    onShow() {
		// 初始化配置
		this.init_config();
		
		uni.setNavigationBarTitle({
		    title: ''
		});
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data_page: 1
        });
        this.get_data_list(1);
    },
    methods: {
		// 初始化配置
		init_config(status) {
		    if ((status || false) == true) {
				var lang_info_temp = this.lang_info;
				const value = uni.getStorageSync('groups_lang_info');
				if (value) {
					lang_info_temp = value;
				}
				this.setData({
					lang_info:lang_info_temp,
				    currency_symbol: app.globalData.get_config('currency_symbol'),
				    common_app_is_online_service: app.globalData.get_config('config.common_app_is_online_service'),
				});
		    } else {
		        app.globalData.is_config(this, 'init_config');
		    }
		},
		
        // 获取数据列表
        init() {
            var self = this; // 加载loding

            this.setData({
                data_list_loding_status: 1
            }); 
	
			// 加载loding
            uni.request({
                url: app.globalData.get_request_url('index', 'index', 'groups'),
                method: 'POST',
                data: {
					shop_id: self.shop_id,
				},
                dataType: 'json',
                success: (res) => {console.log(res);
                    uni.stopPullDownRefresh();
                    self.setData({
                        load_status: 1
                    });

                    if (res.data.code == 0) {
                        var data = res.data.data;
						var lang_info = {};
						if((data.base || null) != null && (data.base.groups_lang_info || null) != null)
						{
							lang_info = data.base.groups_lang_info;
						}
                        self.setData({
                            data_bottom_line_status: true,
                            banner_list: data.banner_list || [],
                            category_list: data.category_list,
							lang_info:lang_info,
							share_info:{
								'title':lang_info.app_title || self.share_info.title,
								'img':(data.banner_list.length > 0) ? (data.banner_list[0].images_url || '') : '',
							},
							data_bottom_line_msg:lang_info.data_bottom_line_msg || '到底啦~',
                        });
						
						uni.setStorageSync('groups_lang_info', lang_info);
						
						uni.setNavigationBarTitle({
						    title: lang_info.app_title || self.share_info.title
						});
						
						this.setData({
						    data_page: 1
						});
						this.get_data_list(1);
                    } else {
                        self.setData({
                            data_list_loding_status: 0,
                            data_bottom_line_status: true
                        });
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.stopPullDownRefresh();
                    self.setData({
                        data_list_loding_status: 2,
                        data_bottom_line_status: true,
                        load_status: 1
                    });
                    app.globalData.showToast(this.lang_info.request_data_error_tips || 'Server request error');
                }
            });
        },
		
		// 分享开启弹层
		popup_share_event(e) {
			if ((this.$refs.share || null) != null) {
				this.$refs.share.init({
					status: true,
					share_info: this.share_info
				});
			}
		},

        // 获取数据列表
        get_data_list(is_mandatory) {
            // 分页是否还有数据
            if ((is_mandatory || 0) == 0) {
                if (this.data_bottom_line_status == true) {
                    return false;
                }
            } 
			
			// 加载loding
            uni.showLoading({
                title: this.lang_info.loading_tips
            });
            this.setData({
                data_list_loding_status: 1
            }); 
			
			// 参数
            var params = this.params;
            var post_data = this.post_data;
            post_data['page'] = this.data_page;
            post_data['category_id'] = params['category_id'] || 0; 
			post_data['shop_id'] = this.shop_id || 0;
			
			// 如果没有分类ID，则说明是首页，首页只展示推荐的
            if (post_data['category_id'] == 0) {
                post_data['is_home_recommended'] = 1;
                post_data['n'] = 1000; //推荐的产品一页调用完，100条足以
            }
			
			// 获取数据
            uni.request({
                url: app.globalData.get_request_url('search', 'index', 'groups'),
                method: 'POST',
                data: post_data,
                dataType: 'json',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();console.log(res);

                    if (res.data.code == 0) {
                        if (res.data.data.data.length > 0) {
                            if (this.data_page <= 1) {
                                var temp_data_list = res.data.data.data;
                            } else {
                                var temp_data_list = this.data_list;
                                var temp_data = res.data.data.data;

                                for (var i in temp_data) {
                                    temp_data_list.push(temp_data[i]);
                                }
                            }

                            this.setData({
                                data_list: temp_data_list,
                                data_total: res.data.data.total,
                                data_page_total: res.data.data.page_total,
                                data_list_loding_status: 3,
                                data_page: this.data_page + 1
                            }); 
							
							// 是否还有数据
                            if (this.data_page > 1 && this.data_page > this.data_page_total) {
                                this.setData({
                                    data_bottom_line_status: true
                                });
                            } else {
                                this.setData({
                                    data_bottom_line_status: false
                                });
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0
                            });

                            if (this.data_page <= 1) {
                                this.setData({
                                    data_list: [],
                                    data_bottom_line_status: false
                                });
                            }
                        }
                    } else {
                        this.setData({
                            data_list_loding_status: 0
                        });
                        app.globalData.showToast(res.data.msg);
                    }
					
					// 分享菜单处理、延时执行，确保基础数据已加载完成
					setTimeout(function() {
					    app.globalData.page_share_handle();
					}, 1000);
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    this.setData({
                        data_list_loding_status: 2
                    });
                    app.globalData.showToast(this.lang_info.request_data_error_tips || 'Server request error');
                }
            });
        },

        // 滚动加载
        scroll_lower(e) {
            this.get_data_list();
        },

        // 导航事件
        navigation_event(e) {
            app.globalData.operation_event(e);
        },

        banner_event(e) {
            app.globalData.operation_event(e);
        }
    }
};
</script>
<style>
@import './index.css';
</style>
